
v = -80.01:0.01:40.01; % voltage axis
df = 140 - v; % driving force for calcium (now arbitrary since we're using GHK)

% Properties 
colWidCM = [85, 114, 174]/10; % in mm
% Markup properties (smaller for insets!)
fontSize = 7;
lineWidth = 0.5;
markerSize = 8;
% Sizes
axWidth = colWidCM(2)/3*0.925;
axHeight = 3.3;



% -- Jahr Stevens Data -- 
kon = @(Mg, v) Mg .* exp( - 0.045.*v - 6.97); % Mg in µM, v in mV, gives units 1/[µM*ms]
koff = @(v) exp(0.017*v + 0.96); % v in mV, gives units 1/ms
mg = 1000;

nmdaInf = @(v) koff(v) ./ (kon(mg,v)+koff(v));
nmdaTau = @(v) 1./(kon(mg,v) + koff(v));

% -- HVA calcium channel from NEURON --
gHVA = @(m,h) m.*m.*h;

mAlpha = @(v) 0.055*(-27 - v) ./ (exp((-27-v)/3.8) - 1);
mBeta = @(v) 0.94 * exp((-75-v)/17);
hAlpha = @(v) 0.000457*exp((-13-v)/50);
hBeta = @(v) 0.0065./(exp((-v-15)/28)+1);

mInf = @(v) mAlpha(v)./(mAlpha(v) + mBeta(v));
hInf = @(v) hAlpha(v)./(hAlpha(v) + hBeta(v));

mTau = @(v) 1./(mAlpha(v) + mBeta(v));
hTau = @(v) 1./(hAlpha(v) + hBeta(v));

dNMDA = @(v,n) koff(v) - (koff(v) + kon(mg, v))*n;
dM = @(v,m) (mInf(v) - m) ./ mTau(v);
dH = @(v,h) (hInf(v) - h) ./ hTau(v);


vgccCol = [0,0.6,0.6];


% -- plotting activation curves and tau --
xLim = [-80 40];
yLim = [0 1];
setBackX = [-0.05 -0.07 -0.065 -0.15];
setBackY = [-10 -12.5 -14 -32];

% Figure 8A
figure(1); clf; hold on;
set(gcf,'units','centimeters','position',[30 30 axWidth axHeight]);
set(gca,'units','centimeters','position',[0.68 0.55 axWidth*0.74 axHeight*0.79]);
plot(v, nmdaInf(v), 'color','k','linewidth',lineWidth);
plot(v, mInf(v), 'color',vgccCol,'linewidth',lineWidth);
xlim([xLim(1)+setBackY(2) xLim(2)]);
ylim([yLim(1)+setBackX(2) yLim(2)]);
% Create Axes
line(xLim,setBackX([1,1]),'color','k','linewidth',lineWidth);
line(xLim(1)+setBackY([1,1]),yLim,'color','k','linewidth',lineWidth);
xticks = -80:40:40;
for xt = xticks
    line([xt,xt],setBackX([2,1]),'color','k','linewidth',lineWidth);
    text(xt,setBackX(3),num2str(xt),'FOntsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
end
text(-20,setBackX(4),'Voltage (mV)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
yticks = 0:0.5:1;
for yt = yticks
    line(xLim(1)+setBackY([2,1]),[yt,yt],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackY(3),yt,num2str(yt),'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(xLim(1)+setBackY(4),0.5,'P(open)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off');
% Make Legend
legPos = [-82 -72 12 0.94 0.84];
line(legPos([1,2]),legPos(4)*[1,1],'Color',vgccCol,'linewidth',lineWidth);
line(legPos([1,2]),legPos(5)*[1,1],'Color','k','linewidth',lineWidth);
text(legPos(1)+legPos(3),legPos(4),'VGCC','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Middle');
text(legPos(1)+legPos(3),legPos(5),'NMDAR','Fontsize',fontSize,'HorizontalAlignment','Left','VerticalAlignment','Middle');

% Figure 8B
xLim = [-80 40];
yLim = [0 8];
setBackX = 8*[-0.05 -0.07 -0.065 -0.15];
setBackY = [-10 -12.5 -14 -26];
figure(2); clf; hold on; 
set(gcf,'units','centimeters','position',[30 23 axWidth axHeight]);
set(gca,'units','centimeters','position',[0.68 0.55 axWidth*0.74 axHeight*0.79]);
plot(v, nmdaTau(v), 'color','k','linewidth',lineWidth);
plot(v, mTau(v), 'color',vgccCol,'linewidth',lineWidth);
xlim([xLim(1)+setBackY(2) xLim(2)]);
ylim([yLim(1)+setBackX(2) yLim(2)]);
% Create Axes
line(xLim,setBackX([1,1]),'color','k','linewidth',lineWidth);
line(xLim(1)+setBackY([1,1]),yLim,'color','k','linewidth',lineWidth);
xticks = -80:40:40;
for xt = xticks
    line([xt,xt],setBackX([2,1]),'color','k','linewidth',lineWidth);
    text(xt,setBackX(3),num2str(xt),'FOntsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
end
text(-20,setBackX(4),'Voltage (mV)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
yticks = 0:4:8;
for yt = yticks
    line(xLim(1)+setBackY([2,1]),[yt,yt],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackY(3),yt,num2str(yt),'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(xLim(1)+setBackY(4),4,'\tau (ms)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off');
% Make Legend
legPos = [30 40 -5 0.94*8 0.84*8];
line(legPos([1,2]),legPos(4)*[1,1],'Color',vgccCol,'linewidth',lineWidth);
line(legPos([1,2]),legPos(5)*[1,1],'Color','k','linewidth',lineWidth);
text(legPos(1)+legPos(3),legPos(4),'VGCC','Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
text(legPos(1)+legPos(3),legPos(5),'NMDAR','Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');


lwProtocol = 0.25;
stepPlotWidth = axWidth * 1.1144; 
stepPlotHeight = axHeight * 2; % fudge factor to account for space in between 8A & 8B
df = @(v) 140 - v;
T = 3;
dt = 0.01;
NT = round(T/dt);
tv = 0:dt:T;

vBase = -70;
stepTime = 0.25;
apWaveDur = 1;
apWaveAmp = -50:5:40;
redIdx = find(apWaveAmp == -10);
purpCol = [180/255,0,1];


NA = length(apWaveAmp);

vtrace = vBase * ones(NT,NA);
n = koff(vBase)/(kon(mg,vBase)+koff(vBase))*ones(NT,NA);
m = mInf(vBase)*ones(NT,NA);
h = hInf(vBase)*ones(NT,NA);
g = gHVA(mInf(vBase),hInf(vBase))*ones(NT,NA);
nCalcium = zeros(size(n));
gCalcium = zeros(size(g));
for na = 1:NA
    idxStart = find(tv>=stepTime,1);
    idxEnd = find(tv>=stepTime+apWaveDur,1);
    % y = (mx)^2, 
    % - we want x to span apWaveDur/2*(-1,1)
    % - and we want y to equal the AP peak at x = apWaveDur/2
    % - so need to find "m" such that this is true...
    squishForPeak = sqrt(apWaveAmp(na)-vBase) / (apWaveDur/2);
    cApWaveT = linspace(-apWaveDur/2,apWaveDur/2,length(idxStart:idxEnd));
    vtrace(idxStart:idxEnd,na) = apWaveAmp(na) - (squishForPeak * cApWaveT).^2;
end
for na = 1:NA
    for nt = 1:NT
        n(nt+1,na) = n(nt,na) + dt*dNMDA(vtrace(nt,na),n(nt,na));
        m(nt+1,na) = m(nt,na) + dt*dM(vtrace(nt,na),m(nt,na));
        h(nt+1,na) = h(nt,na) + dt*dH(vtrace(nt,na),h(nt,na));
        g(nt+1,na) = gHVA(m(nt+1,na),h(nt+1,na));
        
        nCalcium(nt,na) = n(nt,na) * ghk(vtrace(nt,na));
        gCalcium(nt,na) = g(nt,na) * ghk(vtrace(nt,na));
    end
end
nPeakApWaveLong = max(n,[],1);
cPeakApWaveLong = max(g,[],1);
nCaPeakApWaveLong = max(nCalcium, [],1);
gCaPeakApWaveLong = max(gCalcium, [],1);
% Take integral of this signal (subtract the baseline...)
nCaIntegralLong = sum(nCalcium - nCalcium(1,:),1);
gCaIntegralLong = sum(gCalcium - gCalcium(1,:),1);

tvOffset = 3.75;
xLim = [0 T];

setBackX = [-0.08 -0.125 -0.1 -0.28];
setBackY = [-0.15 -0.225 -0.225 -0.7];

reduceProtocol = 0.75;
vShift = [70, 0, 0, 0, 0];
vScale = [1/110*reduceProtocol, 1, 1, 1, 1];
vOffset = [0, 1.25-(1-reduceProtocol), 2.5-(1-reduceProtocol), 3.75-(1-reduceProtocol), 5-(1-reduceProtocol)];

vTrans = @(y, off,scale,shift) off + scale*(y + shift);
vTransC = @(y, off,scale,shift) off + -20*scale*(y + shift);








% Figure 8C
figure(3); clf; hold on; 
set(gcf,'units','centimeters','position',[35 23 stepPlotWidth stepPlotHeight]);
set(gca,'units','centimeters','position',[0.45 0.5 stepPlotWidth*0.85 stepPlotHeight*0.92]);
plot(tv(2:end), vTrans(vtrace(:,1:2:end),vOffset(1),vScale(1),vShift(1)), 'color','k','linewidth',lwProtocol);
plot(tv(2:end), vTrans(vtrace(:,redIdx),vOffset(1),vScale(1),vShift(1)), 'color',purpCol,'linewidth',lwProtocol*2);
plot(tv, vTrans(n(:,1:2:end),vOffset(2),vScale(2),vShift(2)),'color','k','linewidth',lwProtocol);
plot(tv, vTrans(n(:,redIdx),vOffset(2),vScale(2),vShift(2)),'color',purpCol,'linewidth',lwProtocol*2);
plot(tv, vTrans(g(:,1:2:end),vOffset(3),vScale(3),vShift(3)),'color',vgccCol,'linewidth',lwProtocol);
plot(tv, vTrans(g(:,redIdx),vOffset(3),vScale(3),vShift(3)),'color',purpCol,'linewidth',lwProtocol*2);
plot(tv(2:end), vTransC(nCalcium(:,1:2:end),vOffset(4),vScale(4),vShift(4)),'color','k','linewidth',lwProtocol);
plot(tv(2:end), vTransC(nCalcium(:,redIdx),vOffset(4),vScale(4),vShift(4)),'color',purpCol,'linewidth',lwProtocol*2);
plot(tv(2:end), vTransC(gCalcium(:,1:2:end),vOffset(5),vScale(5),vShift(5)),'color',vgccCol,'linewidth',lwProtocol);
plot(tv(2:end), vTransC(gCalcium(:,redIdx),vOffset(5),vScale(5),vShift(5)),'color',purpCol,'linewidth',lwProtocol*2);
xlim([setBackY(2) xLim(2)]);
ylim([setBackX(2) vOffset(5)+1]);
% Create X Axis
line(xLim, setBackX([1,1]),'color','k','linewidth',lineWidth);
xticks = 0:3;
for xt = xticks
    line([xt,xt],setBackX([2,1]),'color','k','linewidth',lineWidth);
    text(xt,setBackX(3),num2str(xt),'FOntsize',fontSize-2,'HorizontalAlignment','Center','VerticalAlignment','Top');
end
text(mean(xLim),setBackX(4),'Time (ms)','Fontsize',fontSize-2,'HorizontalAlignment','Center','VerticalAlignment','Top');
% Create Y Axis : Protocols
line(setBackY([1,1]),vTrans([-70 40],vOffset(1),vScale(1),vShift(1)),'color','k','linewidth',lineWidth);
yticks = [-70 40];
for yt = yticks
    line(setBackY([2,1]),vTrans([yt,yt],vOffset(1),vScale(1),vShift(1)),'color','k','linewidth',lineWidth);
    text(setBackY(3),vTrans(yt,vOffset(1),vScale(1),vShift(1)),num2str(yt),'Fontsize',fontSize-2,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(setBackY(4),vTrans(mean(yticks),vOffset(1),vScale(1),vShift(1)),'mV','Fontsize',fontSize-2,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
% Create Y Axis : NMDAR & VGCC Conductance Traces
line(setBackY([1,1]),vTrans([0 1],vOffset(2),vScale(2),vShift(2)),'color','k','linewidth',lineWidth);
line(setBackY([1,1]),vTrans([0 1],vOffset(3),vScale(3),vShift(3)),'color','k','linewidth',lineWidth);
positionLabel = [vTrans(0.5,vOffset(2),vScale(2),vShift(2)),...
    vTrans(0.5,vOffset(3),vScale(3),vShift(3))];
text(setBackY(4),mean(positionLabel),'P(open)','Fontsize',fontSize-2,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
text(setBackY(3),positionLabel(1),'NMDAR','Fontsize',fontSize-3,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
text(setBackY(3),positionLabel(2),'VGCC','Fontsize',fontSize-3,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
yticks = 0:1;
for yt = yticks
    line(setBackY([2,1]),vTrans([yt,yt],vOffset(2),vScale(2),vShift(2)),'color','k','linewidth',lineWidth);
    text(setBackY(3),vTrans(yt,vOffset(2),vScale(2),vShift(2)),num2str(yt),'Fontsize',fontSize-2,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
for yt = yticks
    line(setBackY([2,1]),vTrans([yt,yt],vOffset(3),vScale(3),vShift(3)),'color','k','linewidth',lineWidth);
    text(setBackY(3),vTrans(yt,vOffset(3),vScale(3),vShift(3)),num2str(yt),'Fontsize',fontSize-2,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
% Create Y Axis : NMDAR & VGCC Calcium Signals
line(setBackY([1,1]),vTrans([0 1],vOffset(4),vScale(4),vShift(4)),'color','k','linewidth',lineWidth);
line(setBackY([1,1]),vTrans([0 1],vOffset(5),vScale(5),vShift(5)),'color','k','linewidth',lineWidth);
positionLabel = [vTrans(0.5,vOffset(4),vScale(4),vShift(4)),...
    vTrans(0.5,vOffset(5),vScale(5),vShift(5))];
text(setBackY(4),mean(positionLabel),'Calcium Current','Fontsize',fontSize-2,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
text(setBackY(3),positionLabel(1),'NMDAR','Fontsize',fontSize-3,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
text(setBackY(3),positionLabel(2),'VGCC','Fontsize',fontSize-3,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
yticks = 0:1;
ylabels = {'0','au'};
for yt = 1:length(yticks)
    line(setBackY([2,1]),vTrans(yticks([yt,yt]),vOffset(4),vScale(4),vShift(4)),'color','k','linewidth',lineWidth);
    text(setBackY(3),vTrans(yticks(yt),vOffset(4),vScale(4),vShift(4)),ylabels{yt},'Fontsize',fontSize-2,'HorizontalAlignment','Right','VerticalAlignment','Middle');
    line(setBackY([2,1]),vTrans(yticks([yt,yt]),vOffset(5),vScale(5),vShift(5)),'color','k','linewidth',lineWidth);
    text(setBackY(3),vTrans(yticks(yt),vOffset(5),vScale(5),vShift(5)),ylabels{yt},'Fontsize',fontSize-2,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
% Expand xLim to include both!
xlim([xLim(1)+setBackY(2) xLim(2)+tvOffset]);


% -- and run short protocol -- 
apWaveDur = 0.5;
NA = length(apWaveAmp);
vtrace = vBase * ones(NT,NA);
n = koff(vBase)/(kon(mg,vBase)+koff(vBase))*ones(NT,NA);
m = mInf(vBase)*ones(NT,NA);
h = hInf(vBase)*ones(NT,NA);
g = gHVA(mInf(vBase),hInf(vBase))*ones(NT,NA);
nCalcium = zeros(size(n));
gCalcium = zeros(size(g));
for na = 1:NA
    idxStart = find(tv>=stepTime,1);
    idxEnd = find(tv>=stepTime+apWaveDur,1);
    % y = (mx)^2, 
    % - we want x to span apWaveDur/2*(-1,1)
    % - and we want y to equal the AP peak at x = apWaveDur/2
    % - so need to find "m" such that this is true...
    squishForPeak = sqrt(apWaveAmp(na)-vBase) / (apWaveDur/2);
    cApWaveT = linspace(-apWaveDur/2,apWaveDur/2,length(idxStart:idxEnd));
    vtrace(idxStart:idxEnd,na) = apWaveAmp(na) - (squishForPeak * cApWaveT).^2;
end
for na = 1:NA
    for nt = 1:NT
        n(nt+1,na) = n(nt,na) + dt*dNMDA(vtrace(nt,na),n(nt,na));
        m(nt+1,na) = m(nt,na) + dt*dM(vtrace(nt,na),m(nt,na));
        h(nt+1,na) = h(nt,na) + dt*dH(vtrace(nt,na),h(nt,na));
        g(nt+1,na) = gHVA(m(nt+1,na),h(nt+1,na));
        
        nCalcium(nt,na) = n(nt,na) * ghk(vtrace(nt,na));
        gCalcium(nt,na) = g(nt,na) * ghk(vtrace(nt,na));
    end
end
nPeakApWaveShort = max(n,[],1);
cPeakApWaveShort = max(g,[],1);
nCaPeakApWaveShort = max(nCalcium, [],1);
gCaPeakApWaveShort = max(gCalcium, [],1);
% Take integral of this signal (subtract the baseline...)
nCaIntegralShort = sum(nCalcium - nCalcium(1,:),1);
gCaIntegralShort = sum(gCalcium - gCalcium(1,:),1);
plot(tvOffset+tv(2:end), vTrans(vtrace(:,1:2:end),vOffset(1),vScale(1),vShift(1)), 'color','k','linewidth',lwProtocol);
plot(tvOffset+tv(2:end), vTrans(vtrace(:,redIdx),vOffset(1),vScale(1),vShift(1)), 'color','r','linewidth',lwProtocol*2);
plot(tvOffset+tv, vTrans(n(:,1:2:end),vOffset(2),vScale(2),vShift(2)),'color','k','linewidth',lwProtocol);
plot(tvOffset+tv, vTrans(n(:,redIdx),vOffset(2),vScale(2),vShift(2)),'color','r','linewidth',lwProtocol*2);
plot(tvOffset+tv, vTrans(g(:,1:2:end),vOffset(3),vScale(3),vShift(3)),'color',vgccCol,'linewidth',lwProtocol);
plot(tvOffset+tv, vTrans(g(:,redIdx),vOffset(3),vScale(3),vShift(3)),'color','r','linewidth',lwProtocol*2);
plot(tvOffset+tv(2:end), vTransC(nCalcium(:,1:2:end),vOffset(4),vScale(4),vShift(4)),'color','k','linewidth',lwProtocol);
plot(tvOffset+tv(2:end), vTransC(nCalcium(:,redIdx),vOffset(4),vScale(4),vShift(4)),'color','r','linewidth',lwProtocol*2);
plot(tvOffset+tv(2:end), vTransC(gCalcium(:,1:2:end),vOffset(5),vScale(5),vShift(5)),'color',vgccCol,'linewidth',lwProtocol);
plot(tvOffset+tv(2:end), vTransC(gCalcium(:,redIdx),vOffset(5),vScale(5),vShift(5)),'color','r','linewidth',lwProtocol*2);
% Create X Axis
line(tvOffset+xLim, setBackX([1,1]),'color','k','linewidth',lineWidth);
xticks = 0:3;
for xt = xticks
    line(tvOffset+[xt,xt],setBackX([2,1]),'color','k','linewidth',lineWidth);
    text(tvOffset+xt,setBackX(3),num2str(xt),'FOntsize',fontSize-2,'HorizontalAlignment','Center','VerticalAlignment','Top');
end
text(tvOffset+mean(xLim),setBackX(4),'Time (ms)','Fontsize',fontSize-2,'HorizontalAlignment','Center','VerticalAlignment','Top');
% Create Y Axis : Protocols
line(tvOffset+setBackY([1,1]),vTrans([-70 40],vOffset(1),vScale(1),vShift(1)),'color','k','linewidth',lineWidth);
yticks = [-70 40];
for yt = yticks
    line(tvOffset+setBackY([2,1]),vTrans([yt,yt],vOffset(1),vScale(1),vShift(1)),'color','k','linewidth',lineWidth);
end
% Create Y Axis : NMDAR Conductance
line(tvOffset+setBackY([1,1]),vTrans([0 1],vOffset(2),vScale(2),vShift(2)),'color','k','linewidth',lineWidth);
yticks = 0:1;
for yt = yticks
    line(tvOffset+setBackY([2,1]),vTrans([yt,yt],vOffset(2),vScale(2),vShift(2)),'color','k','linewidth',lineWidth);
end
% Create Y Axis : VGCC Conductance
line(tvOffset+setBackY([1,1]),vTrans([0 1],vOffset(3),vScale(3),vShift(3)),'color','k','linewidth',lineWidth);
yticks = 0:1;
for yt = yticks
    line(tvOffset+setBackY([2,1]),vTrans([yt,yt],vOffset(3),vScale(3),vShift(3)),'color','k','linewidth',lineWidth);
end
% Create Y Axis : NMDAR Calcium
line(tvOffset+setBackY([1,1]),vTrans([0 1],vOffset(4),vScale(4),vShift(4)),'color','k','linewidth',lineWidth);
yticks = 0:1;
for yt = yticks
    line(tvOffset+setBackY([2,1]),vTrans([yt,yt],vOffset(4),vScale(4),vShift(4)),'color','k','linewidth',lineWidth);
end
% Create Y Axis : VGCC Calcium
line(tvOffset+setBackY([1,1]),vTrans([0 1],vOffset(5),vScale(5),vShift(5)),'color','k','linewidth',lineWidth);
yticks = 0:1;
for yt = yticks
    line(tvOffset+setBackY([2,1]),vTrans([yt,yt],vOffset(5),vScale(5),vShift(5)),'color','k','linewidth',lineWidth);
end
set(gca,'visible','off');
text(tvOffset+3,vTrans(-60,vOffset(1),vScale(1),vShift(1)),'protocol','Fontsize',fontSize-2,'HorizontalAlignment','Right','VerticalALignment','Bottom');
text(3,vTrans(-60,vOffset(1),vScale(1),vShift(1)),'protocol','Fontsize',fontSize-2,'HorizontalAlignment','Right','VerticalALignment','Bottom');
text(3,vTrans(-40,vOffset(1),vScale(1),vShift(1)),'1 ms','Fontsize',fontSize-2,'HorizontalAlignment','Right','VerticalALignment','Bottom');
text(tvOffset+3,vTrans(-40,vOffset(1),vScale(1),vShift(1)),'0.5 ms','Fontsize',fontSize-2,'HorizontalAlignment','Right','VerticalALignment','Bottom');




% Figure 8D
xLim = [-50 40];
yLim = [0 1];
setBackX = [-0.05 -0.065 -0.06 -0.16];
setBackY = [-5 -7 -7.75 -12];
redIdx = find(apWaveAmp==-10);
figure(4); clf; hold on;
set(gcf,'units','centimeters','position',[40 30 axWidth axHeight]);
set(gca,'units','centimeters','position',[0.6 0.54 axWidth*0.825 axHeight*0.78]);
plot(apWaveAmp, nPeakApWaveLong, 'color','k','linewidth',lineWidth,'linestyle','-');
plot(apWaveAmp, cPeakApWaveLong, 'color',vgccCol,'linewidth',lineWidth,'linestyle','-');
plot(apWaveAmp, nPeakApWaveShort, 'color','k','linewidth',lineWidth,'linestyle',':');
plot(apWaveAmp, cPeakApWaveShort, 'color',vgccCol,'linewidth',lineWidth,'linestyle',':');
plot(apWaveAmp(redIdx), nPeakApWaveLong(redIdx), 'color',purpCol,'marker','.','markerSize',markerSize);
plot(apWaveAmp(redIdx), cPeakApWaveLong(redIdx), 'color',purpCol,'marker','.','markerSize',markerSize);
plot(apWaveAmp(redIdx), nPeakApWaveShort(redIdx), 'color','r','marker','.','markerSize',markerSize);
plot(apWaveAmp(redIdx), cPeakApWaveShort(redIdx), 'color','r','marker','.','markerSize',markerSize);
xlim([xLim(1)+setBackY(2) xLim(2)]);
ylim([yLim(1)+setBackX(2) yLim(2)]);
% Create Axes
line(xLim,setBackX([1,1]),'color','k','linewidth',lineWidth);
line(xLim(1)+setBackY([1,1]),yLim,'color','k','linewidth',lineWidth);
xticks = -50:25:40;
for xt = xticks
    line([xt,xt],setBackX([2,1]),'color','k','linewidth',lineWidth);
    text(xt,setBackX(3),num2str(xt),'FOntsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
end
text(-5,setBackX(4),'Voltage (mV)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
yticks = 0:1;
for yt = yticks
    line(xLim(1)+setBackY([2,1]),[yt,yt],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackY(3),yt,num2str(yt),'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(xLim(1)+setBackY(4),0.5,'Peak P(open)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off');
% Make Legend
legPos = [-50 6 2.5 0.95 -0.1];
legNames = {'VGCC','NMDAR','1 ms','0.5 ms'};
legCol = {vgccCol,'k','k','k'};
legStyle = {'-','-','-',':'};
for leg = 1:4
    line(legPos(1)+[0 legPos(2)],legPos(4)+(leg-1)*legPos(5)*[1,1],'color',legCol{leg},'linewidth',lineWidth,'linestyle',legStyle{leg});
    text(sum(legPos(1:3)),legPos(4)+(leg-1)*legPos(5),legNames{leg},'Fontsize',fontSize-1,'HorizontalAlignment','Left','VerticalAlignment','Middle');
end


% Figure 8E
xLim = [-50 40];
yLim = [0 1];
setBackX = [-0.05 -0.065 -0.06 -0.16];
setBackY = [-5 -7 -7.75 -12];
redIdx = find(apWaveAmp==-10);
caPeakScale = 1/min(gCaIntegralLong) * 1.5;
figure(5); clf; hold on;
set(gcf,'units','centimeters','position',[40 23 axWidth axHeight]);
set(gca,'units','centimeters','position',[0.6 0.54 axWidth*0.825 axHeight*0.78]);
plot(apWaveAmp, caPeakScale*nCaIntegralLong, 'color','k','linewidth',lineWidth,'linestyle','-');
plot(apWaveAmp, caPeakScale*gCaIntegralLong, 'color',vgccCol,'linewidth',lineWidth,'linestyle','-');
plot(apWaveAmp, caPeakScale*nCaIntegralShort, 'color','k','linewidth',lineWidth,'linestyle',':');
plot(apWaveAmp, caPeakScale*gCaIntegralShort, 'color',vgccCol,'linewidth',lineWidth,'linestyle',':');
plot(apWaveAmp(redIdx), caPeakScale*nCaIntegralLong(redIdx), 'color',purpCol,'marker','.','markerSize',markerSize);
plot(apWaveAmp(redIdx), caPeakScale*gCaIntegralLong(redIdx), 'color',purpCol,'marker','.','markerSize',markerSize);
plot(apWaveAmp(redIdx), caPeakScale*nCaIntegralShort(redIdx), 'color','r','marker','.','markerSize',markerSize);
plot(apWaveAmp(redIdx), caPeakScale*gCaIntegralShort(redIdx), 'color','r','marker','.','markerSize',markerSize);
xlim([xLim(1)+setBackY(2) xLim(2)]);
ylim([yLim(1)+setBackX(2) yLim(2)]);
% Create Axes
line(xLim,setBackX([1,1]),'color','k','linewidth',lineWidth);
line(xLim(1)+setBackY([1,1]),yLim,'color','k','linewidth',lineWidth);
xticks = -50:25:40;
for xt = xticks
    line([xt,xt],setBackX([2,1]),'color','k','linewidth',lineWidth);
    text(xt,setBackX(3),num2str(xt),'FOntsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
end
text(-5,setBackX(4),'Voltage (mV)','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Top');
yticks = 0:1;
ylabels = {'0','au'};
for yt = yticks
    line(xLim(1)+setBackY([2,1]),[yt,yt],'color','k','linewidth',lineWidth);
    text(xLim(1)+setBackY(3),yt,ylabels{yt+1},'Fontsize',fontSize,'HorizontalAlignment','Right','VerticalAlignment','Middle');
end
text(xLim(1)+setBackY(4),0.5,'Integrated I_{Ca}','Fontsize',fontSize,'HorizontalAlignment','Center','VerticalAlignment','Bottom','Rotation',90);
set(gca,'visible','off');
% Make Legend
legPos = [-50 6 2.5 0.95 -0.1];
legNames = {'VGCC','NMDAR','1 ms','0.5 ms'};
legCol = {vgccCol,'k','k','k'};
legStyle = {'-','-','-',':'};
for leg = 1:4
    line(legPos(1)+[0 legPos(2)],legPos(4)+(leg-1)*legPos(5)*[1,1],'color',legCol{leg},'linewidth',lineWidth,'linestyle',legStyle{leg});
    text(sum(legPos(1:3)),legPos(4)+(leg-1)*legPos(5),legNames{leg},'Fontsize',fontSize-1,'HorizontalAlignment','Left','VerticalAlignment','Middle');
end










